//
//  WBStatusPictureView.swift
//  WeiBoApp
//
//  Created by James on 2017/7/19.
//  Copyright © 2017年 James. All rights reserved.
//

import UIKit

class WBStatusPictureView: UIView {
    
    ///配图试图数组
    var urls:[WBStatusPicture]? {
        didSet{
            //1.隐藏所有的imageView
            for v in subviews {
                v.isHidden = true
            }
            
            //遍历设置数组
            var index = 0
            for url in urls ?? [] {
                
                let iv = subviews[index] as! UIImageView
                iv.contentMode = .scaleAspectFill
                iv.clipsToBounds = true
                iv.hzj_setImage(urlString: url.thumbnail_pic!, placeholdImage: nil)
                iv.isHidden = false
                
                
                if index == 1 && urls?.count == 4{
                    index += 1
                }

                index += 1
            }
        }
    }
    
    @IBOutlet weak var heightCons:NSLayoutConstraint!

    override func awakeFromNib() {
        setupUI()
    }

}

extension WBStatusPictureView {
    fileprivate func setupUI(){
        clipsToBounds = true //超出边界的内容不显示
        backgroundColor = superview?.backgroundColor
        let count = 9
        let rect = CGRect(x: 0, y: WBStatusPictureOutterMargin, width: WBStatusPictureItemWidth, height: WBStatusPictureItemWidth)
        for i in 0 ..< count {
            let iv = UIImageView()
            
            let row = CGFloat(i/3)//行
            let col = CGFloat(i%3)//列
            
            iv.backgroundColor = UIColor.red
            
            let xoffset = row * (WBStatusPictureItemWidth + WBStatusPictureViewInnerMargin)
            let yoffset = col * (WBStatusPictureItemWidth + WBStatusPictureViewInnerMargin)
            
            iv.frame = rect.offsetBy(dx: xoffset, dy: yoffset)
            addSubview(iv)
        }
        
    }
}
